﻿using System.Collections.Generic;
using PerceptiveMCAPI.Types;

namespace PerceptiveMCAPI
{
   internal static class generateText_Validator
   {
      // generateText_type
      internal static Api_ValidatorMessage type_valid( EnumValues.generateText_type generateTextType )
      {
         if ( generateTextType == EnumValues.generateText_type.NotSpecified )
         {
            return Validator.FormatMsg( EnumValues.MessageLevel.Error, "type", "The content 'type' was not specified." );
         }
         return null;
      }
      // generateTextContent
      internal static Api_ValidatorMessage content_valid( object content, EnumValues.generateText_type type )
      {
         //------------------------------------------------------------- get rid of edge cases
         if ( type == EnumValues.generateText_type.NotSpecified )
            return null;   // ignore, will catch in 'generateTextType_valid' validator

         if ( content == null )
            return Validator.FormatMsg( EnumValues.MessageLevel.Error, "content",
               "Required parameter 'content' is null or empty" );

         // -- check for type
         switch ( type )
         {
            case EnumValues.generateText_type.cid:
            case EnumValues.generateText_type.tid:
            case EnumValues.generateText_type.html:
            case EnumValues.generateText_type.url:
               if ( content.GetType().Name.ToLower() != "string" )
                  return Validator.parameter_type_valid( "content", "string", content );
               break;
            case EnumValues.generateText_type.template:
               string type_name = new Dictionary<string, string>().GetType().Name.ToLower();
               if ( content.GetType().Name.ToLower() != type_name )
                  return Validator.parameter_type_valid( "content", type_name, content );
               break;
         }

         // -- check for value
         switch ( type )
         {
            case EnumValues.generateText_type.cid:
               return Validator.string_value_required( content.ToString().Trim(), "content [cid]" );
            case EnumValues.generateText_type.tid:
               return Validator.string_value_required( content.ToString().Trim(), "content [tid]" );
            case EnumValues.generateText_type.html:
               return Validator.string_value_required( content.ToString().Trim(), "content [html]" );
            case EnumValues.generateText_type.url:
               return Validator.url_valid( content.ToString().Trim() );
            case EnumValues.generateText_type.template:
               Dictionary<string, string> dict = content as Dictionary<string, string>;
               if ( dict.Count < 1 )
                  return Validator.FormatMsg( EnumValues.MessageLevel.Error, "content [template]",
                     "There are no entries in the template section dictionary" );
               //
               foreach ( KeyValuePair<string, string> kvp in dict )
               {
                  if ( string.IsNullOrEmpty( kvp.Key ) || string.IsNullOrEmpty( kvp.Value ) )
                  {
                     return Validator.FormatMsg( EnumValues.MessageLevel.Error, "content [template]",
                        "One or more section entries are invalid" );
                  }
               }
               break;
         }
         return null;
      }

   }
}
